package com.sjgtw.cloud.system.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sjgtw.cloud.common.core.constant.UserConstants;
import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.system.api.domain.SysDept;
import com.sjgtw.cloud.system.mapper.SysDeptMapper;
import com.sjgtw.cloud.system.service.ISysOrganizationProvisioningService;

/**
 * <p>Title: 组织机构部门信息管理--服务层--接口实现</p>
 * <p>Description: rsf数据同步</p>
 * @author Admin
 * @date 2020-7-7
 */
@Service
public class SysOrganizationProvisioningServiceImpl implements ISysOrganizationProvisioningService
{
	private static final Logger log = LoggerFactory.getLogger(SysOrganizationProvisioningServiceImpl.class);

	@Autowired
	private SysDeptMapper deptMapper;

	/**
	 * 新增保存部门信息
	 *
	 * @param dept 部门信息
	 * @return 结果
	 */
	@Override
	public int insertDept(SysDept dept) {
		SysDept info = deptMapper.selectDeptById(dept.getParentId());
		if(StringUtils.isNotNull(info))
		{
			// 如果父节点不为正常状态,则不允许新增子节点
			if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
				//throw new CustomException("部门停用，不允许新增");
				return 0;
			}
			dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
		}
		//若为空则会丢失Ancestors字段，Ancestors字段为空；到时需要通过修改人工不全本字段；
		return deptMapper.insertDept(dept);
	}

	/**
	 * 修改保存部门信息
	 *
	 * @param dept 部门信息
	 * @return 结果
	 */
	@Override
	public int updateDept(SysDept dept) {
		SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
		SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
		if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) {
			String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
			String oldAncestors = oldDept.getAncestors();
			dept.setAncestors(newAncestors);
			updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
		}
		int result = deptMapper.updateDept(dept);
		if (UserConstants.DEPT_NORMAL.equals(dept.getStatus())) {
			// 如果该部门是启用状态，则启用该部门的所有上级部门
			updateParentDeptStatus(dept);
		}
		return result;
	}

	/**
	 * 修改该部门的父级部门状态
	 *
	 * @param dept 当前部门
	 */
	private void updateParentDeptStatus(SysDept dept) {
		String updateBy = dept.getUpdateBy();
		dept = deptMapper.selectDeptById(dept.getDeptId());
		if(StringUtils.isNotNull(dept.getAncestors()))
		{
			dept.setUpdateBy(updateBy);
			deptMapper.updateDeptStatus(dept);
		}
	}

	/**
	 * 修改子元素关系
	 *
	 * @param deptId       被修改的部门ID
	 * @param newAncestors 新的父ID集合
	 * @param oldAncestors 旧的父ID集合
	 */
	public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) {
		List<SysDept> children = selectChildrenDeptById(deptId);
		for (SysDept child : children) {
			child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
		}
		if (children.size() > 0) {
			deptMapper.updateDeptChildren(children);
		}
	}

	/**
	 * 删除部门管理信息
	 *
	 * @param deptId 部门ID
	 * @return 结果
	 */
	@Override
	public int deleteDeptById(Long deptId) {
		return deptMapper.deleteDeptById(deptId);
	}

	/**
	 * 根据ID查询所有子部门
	 *
	 * @param deptId 部门ID
	 * @return 部门列表
	 */
	public List<SysDept> selectChildrenDeptById(Long deptId)
	{
		List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
		return children;
	}
}
